home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11034 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  79 lines

  1. Path: deneb.dur.ac.uk!d40p5m
  2. From: Matthew Strawbridge <M.J.Strawbridge@durham.ac.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: GNU question
  5. Date: Tue, 12 Mar 1996 11:41:32 +0000
  6. Organization: University of Durham, Durham, UK.
  7. Message-ID: <Pine.SOL.3.91-941213.960312113456.29919A-100000@deneb.dur.ac.uk>
  8. NNTP-Posting-Host: deneb.dur.ac.uk
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11.  
  12.  
  13. Hi,
  14. I'm new to C++ programming, and rather than try "hello world" I thought 
  15. I'd try to write something a bit more useful! Needless to say, it doesn't 
  16. run, but hopefully some kind soul out there will be able to point out 
  17. where I've gone wrong.
  18.  
  19. This program is supposed to generate a random number, then read a file of 
  20. one line quotes and save one of them to another file - so that I can 
  21. reference it on my WEB page. I wrote the program with a book on Turbo 
  22. C++, but have to compile it with GNU c++ on unix - hopefully the only 
  23. problem is I've not included the right files, or randomize() and random() 
  24. are differently named (I get 'implicit definition' errors on the lines 
  25. that use these).
  26.  
  27. Here is the code:
  28. // quote.cc
  29. // Generates a random quote from the file quotes.dat
  30. // Puts the result in quote.of.the.day
  31.  
  32. // Matthew Strawbridge
  33. // March '96
  34.  
  35. #include <stdlib.h>             // for rand (), randomize ()
  36. #include <time.h>               // for randomize ()
  37. #include <fstream.h>            // for file streams
  38.  
  39. void main()
  40.         {
  41.         const int MAX = 80;     // size of the buffer
  42.         char buffer[MAX];       // character buffer
  43.         int j, lines, r;
  44.  
  45.         ifstream infile("quotes.dat");  // file for input
  46.         randomize();            // seed random number generator
  47.  
  48.         // Count lines in file
  49.         lines = 0;
  50.         while (!infile.eof() )
  51.                 {
  52.                 infile.getline(buffer,MAX);
  53.                 lines++;
  54.                 }
  55.         infile.close();
  56.  
  57.         ifstream infile2("quotes.dat");         // Open the file I just 
  58. closed
  59.         ofstream outfile("quote.of.the.day");   // File for output
  60.         r = random(lines);
  61.  
  62.         for (j = 0; j < r; ++j )
  63.                 {
  64.                 infile2.getline(buffer,MAX);    // Read to random point
  65.                 }
  66.  
  67.         outfile << buffer;
  68.  
  69.         }
  70.  
  71. Many thanks,
  72.         Mat.
  73.  
  74. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75.           Matthew Strawbridge : M.J.Strawbridge@durham.ac.uk
  76.                           WWW : http://www.dur.ac.uk/~d40p5m
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78.  
  79.